define(['template/weather/indexTpl','wx.ajax','wx.tpl'],function(tpl,wx){
    //http://www.heweather.com/documents/api
	var weather = {};
	var daily = ['日','一','二','三','四','五','六'];

    function init(id){
        var t = new Date().getTime();
        $.when(
            $.ajax('/res/css/weather.css?_='+t))
            .done(function(css){
                $('<style type="text/css">').append(css).appendTo('head');
                forecast();
            });
	}

	function forecast(){
		wx.sendData('/weather/get',function(data){
        	data = data.result['HeWeather data service 3.0'];
        	var bg = 'qing';
        	var code = data[0].now.cond.code;
        	var sugg = data[0].suggestion;
        	var bar = '<strong>舒适指数：</strong>'+sugg.comf.txt+'&nbsp;&nbsp;<strong>洗车指数：</strong>'+
        					sugg.cw.txt+'&nbsp;&nbsp;<strong>穿衣指数：</strong>'+
        					sugg.drsg.txt+'&nbsp;&nbsp;<strong>旅游指数：</strong>'+
        					sugg.trav.txt+'&nbsp;&nbsp;<strong>感冒指数：</strong>'+
        					sugg.flu.txt+'&nbsp;&nbsp;<strong>运动指数：</strong>'+
        					sugg.sport.txt+'&nbsp;&nbsp;<strong>紫外线指数：</strong>'+
        					sugg.uv.txt;
        	$('#window-bar-weather').html('<marquee>'+bar+'</marquee>');
        	for(var i=0;i<data[0].daily_forecast.length;i++){
        		data[0].daily_forecast[i].date = daily[new Date(data[0].daily_forecast[i].date).getDay()];
        	}
        	if(code==104){
        		bg = 'yin';
        	} else if(code >100 && code<300){
        		bg = 'feng';
        	} else if(code >300 && code<400){
        		bg = 'yu';
        	} else if(code >400 && code<500){
        		bg = 'xue';
        	} else if(code >500 && code<600){
        		bg = 'mai';
        	} 
        	$('#window-frame-weather')
        		.addClass('weather-bg')
        		.css({'background-image':'url(/res/images/weather/'+bg+'.jpg)','background-size':'100% '+$('#window-frame-weather').height()+'px'})
    			.html(wx.tpl(tpl.forecast,{d:data[0]}));
		});
	}

	function brief(){
		wx.sendData('/weather/get',function(data){
	        data = data.result['HeWeather data service 3.0'];
	        $.jGrowl(wx.tpl(tpl.brief,{d:data[0]}), {life: 7000});
		});
	}
	weather.init = init;
	weather.brief = brief;
    return weather;
});